package com.chinaunicom.app.controller;

import com.chinaunicom.app.util.AppConstant;
import com.chinaunicom.app.util.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: ruiyanglee
 * @date: 2020-09-15 09:50
 * @description:
 */
@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private RestTemplate restTemplate;

    @ResponseBody
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        //获取ticker
        String ticket = CookieUtil.getCookie(request, "ticket");
        //请求sso-server退出登录
        boolean isLogout = restTemplate.getForObject(AppConstant.SSO_URL + "logout?ticket=" + ticket, Boolean.class);
        if (isLogout) {
            //清除cookie
            CookieUtil.setCookie(response, "ticket", "", 0);
            return "用户已登出";
        }
        return "登出失败";
    }

    @GetMapping("/getMsg")
    public Object getMsg() {
        // TODO 获取信息
        return "visit app1 website";
    }

    @GetMapping("/info")
    public Object getInfo() {
        // TODO 获取信息
        return "app1 test sso server";
    }
}
